package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* 套餐管理
* */


@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     *新增套餐接口
     * @author Mosetian
     * @date 2022-9-15 16:36
     * @param	setmealDto
     * @return R
     */
    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R  addSetmeal(@RequestBody SetmealDto setmealDto){
        R r = setmealService.saveSetmealWithDish(setmealDto);
        return r;
    }

    /**
     * 套餐分页查询
     * @author Mosetian
     * @date 2022-9-15 17:05
     * @param	page
     * @param	pageSize
     * @param	name
     * @return R
     */
    @GetMapping("/page")
    public R pageQuery(int page, int pageSize, String name){
        R result = setmealService.pageWithDish(page, pageSize, name);
        return result;
    }

    /**
     * 删除套餐
     * @author Mosetian
     * @date 2022-9-15 17:13
     * @param	ids
     * @return R
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R deleteSetmeal(@RequestParam List<Long> ids){
        R r = setmealService.deleteWithDish(ids);
        return r;
    }

    /**
     * 起售/停售套餐
     * @author Mosetian
     * @date 2022-9-15 17:27
     * @param	status
     * @param	ids
     * @return R
     */
    @PostMapping("/status/{status}")
    public R updateStatusByIds(@PathVariable Integer status,@RequestParam List<Long> ids){
        LambdaUpdateWrapper<Setmeal> qw = new LambdaUpdateWrapper<>();
        qw.set(Setmeal::getStatus, status);
        qw.in(Setmeal::getId, ids);
        setmealService.update(qw);

        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }

        return R.success("操作成功");
    }

    /**
     * 根据套餐id查询套餐数据,用于回显
     * @author Mosetian
     * @date 2022-9-15 17:31
     * @param	id
     * @return R<SetmealDto>
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getByIdForUpdate(@PathVariable Long id){
        R<SetmealDto> setmealDto = setmealService.getByIdWithDish(id);
        return setmealDto;
    }

    /**
     * 修改套餐
     * @author Mosetian
     * @date 2022-9-15 21:52
     * @param	setmealDto
     * @return R
     */
    @PutMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R updateSetmealWithDish(@RequestBody SetmealDto setmealDto){

        setmealService.updateWithDish(setmealDto);

        return R.success("修改套餐成功");
    }

    /**
     * 根据套餐分类id查询该套餐分类下的套餐
     * @author Mosetian
     * @date 2022-9-16 18:52
     * @param	setmeal
     * @return R<List<Setmeal>>
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> listSetmealWithCategory(Setmeal setmeal){

        R r = setmealService.listWithCategory(setmeal);

        return r;
    }
}